package com.tyxuan.controller;

import com.tyxuan.config.DefaultUser;
import com.tyxuan.model.Result;
import com.tyxuan.model.StuInfo;
import com.tyxuan.model.Student;
import com.tyxuan.model.User;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@RestController
public class UserController {

    private final Pattern nameRule = Pattern.compile("^[a-zA-Z0-9]{4,10}$");
    private final Pattern pswdRule = Pattern.compile("^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{6,12}$");

    private final Map<String, UUID> userOnLine = new HashMap<>(4);
    private final Map<Integer, Student> students = new HashMap<>(128);
    private final Map<String, StuInfo> stuInfos = new HashMap<>(128);

    {
        stuInfos.put("Lo.pin", new StuInfo("Lo.pin", "男", 15, "Do.p",
                "Ma.l", "北京", "2024-04-01", "136-5986-1447"));
    }

    @PostMapping("/login")
    public DefaultUser login() {
        return new DefaultUser(123, "meicai", "123456", Arrays.asList(Pet.B));
    }

    @PostMapping("/userLogin")
    public Result login(@RequestBody() User user) {
        String username = user.getUsername();
        if (StringUtils.isEmpty(username)) {
            return Result.failure("用户名为空");
        }
        if (!nameRule.matcher(username).matches()) {
            return Result.failure("用户名4-10位字母、数字");
        }

        String password = user.getPassword();
        if (StringUtils.isEmpty(password)) {
            return Result.failure("密码为空");
        }
        if (!pswdRule.matcher(password).matches()) {

            return Result.failure("要求6-12位密码, 至少包含字母、数字、特殊字符，1-9位");
        }

        UUID token = userOnLine.getOrDefault(username, UUID.randomUUID());
        userOnLine.put(username, token);
        return Result.success(token);
    }

    @PostMapping("/students")
    public Result getStu(@RequestBody @Nullable Student stu) {
        if (students.size() == 0) {
            SecureRandom rad = new SecureRandom();
            for (int i = 0; i < 115; i++) {
                int id = rad.nextInt(555);
                students.put(i, new Student(i, "学员-" + id, rad.nextInt(18),
                        String.valueOf(rad.nextInt(2)), id, rad.nextInt(5),
                        String.valueOf(rad.nextInt(3)), "地址-" + id, "13687891" + id
                ));
            }
        }
        Collection<Student> studentRs = students.values();
        if (stu != null) {
            studentRs = studentRs.stream()
                    .filter(student -> student.getName().contains(stu.getName()))
                    .collect(Collectors.toList());
        }
        return Result.success(studentRs);
    }


    @DeleteMapping("/students/{id}")
    public Result getStu(@PathVariable int id) {
        Student student = students.remove(id);
        return student == null ? Result.failure("No such student id") : Result.success(student.getName());
    }

    @PostMapping("/students/info")
    public Result getStuInfo(@RequestBody StuInfo info) {

        stuInfos.put(info.getName(), info);
        return Result.success(info.getName());
    }

    @GetMapping("/students/info")
    public Result getStuInfos() {
        return Result.success(stuInfos.values());
    }

    @PutMapping("/students/info")
    public Result UpdateStuInfos(@RequestBody StuInfo info) {
        stuInfos.put(info.getName(), info);
        return Result.success(info.getName());
    }


    @DeleteMapping("/students/info/{name}")
    public Result UpdateStuInfos(@PathVariable String name) {
        StuInfo stuInfo = stuInfos.remove(name);
        if (stuInfo != null) {
            return Result.success(stuInfo.getName());
        } else
            return Result.failure("no such user info: " + name);
    }

}



